var e = require;
var t = module;
var n = exports;
Object.defineProperty(n, "__esModule", {
    value: !0
});
n.GIFCache = n.GIF = n.FileHead = n.FileType = void 0;
var o, i = e("LZW"); (function(e) {
    e[e.UNKNOWN = 0] = "UNKNOWN";
    e[e.PNG = 1] = "PNG";
    e[e.JPG = 2] = "JPG";
    e[e.GIF = 3] = "GIF";
    e[e.WEBP = 4] = "WEBP"
})(o = n.FileType || (n.FileType = {}));
var a = function() {
    function e() {}
    e.IMAGE_PNG = "89504e47";
    e.IMAGE_JPG = "ffd8ff";
    e.IMAGE_GIF = "474946";
    e.RIFF = "52494646";
    e.WEBP_RIFF = e.RIFF;
    e.WEBP_WEBP = "57454250";
    return e
} ();
n.FileHead = a;
var r = function() {
    function e() {
        this._offset = 0;
        this._info = {
            header: "",
            frames: [],
            comment: ""
        };
        this._delays = [];
        this._spriteFrames = [];
        this._canvas = null;
        this._context = null;
        this.id = "GIF";
        this.async = !0
    }
    Object.defineProperty(e.prototype, "buffer", {
        get: function() {
            return this._buffer
        },
        set: function(e) {
            this.clear();
            this._buffer = e;
            this._view = new Uint8Array(e)
        },
        enumerable: !1,
        configurable: !0
    });
    e.prototype.handle = function(e, t) {
        this.buffer = e.content;
        this.getHeader();
        this.getScrDesc();
        this.getTexture();
        0 == this._spriteFrames.length ? t(new Error("gif加载失败,帧长度为0")) : t(null, {
            delays: this._delays,
            spriteFrames: this._spriteFrames,
            length: this._info.frames.length
        })
    };
    e.detectFormat = function(e) {
        return - 1 != e.indexOf(a.IMAGE_GIF) ? o.GIF: -1 != e.indexOf(a.IMAGE_PNG) ? o.PNG: -1 != e.indexOf(a.IMAGE_JPG) ? o.JPG: -1 != e.indexOf(a.WEBP_RIFF) && -1 != e.indexOf(a.WEBP_WEBP) ? o.WEBP: o.UNKNOWN
    };
    e.bytes2HexString = function(e) {
        for (var t = "",
        n = 0; n < e.length; n++) {
            var o, i = e[n];
            1 == (o = i < 0 ? (255 + i + 1).toString(16) : i.toString(16)).length && (o = "0" + o);
            t += o
        }
        return t
    };
    e.prototype.getTexture = function() {
        for (var e = 0,
        t = 0,
        n = this._info.frames; t < n.length; t++) {
            var o = n[t];
            this.decodeFrame2Texture(o, e++)
        }
    };
    e.prototype.getSpriteFrame = function(e) {
        return this._spriteFrames[e] ? this._spriteFrames[e] : this.decodeFrame2Texture(this._info.frames[e], e)
    };
    e.prototype.decodeFrame = function(e) {
        var t = this._context.getImageData(e.img.x, e.img.y, e.img.w, e.img.h);
        e.img.m ? this._tab = e.img.colorTab: this._tab = this._info.colorTab;
        i.default.decode(e.img.srcBuf, e.img.codeSize).forEach(function(n, o) {
            t.data[4 * o] = this._tab[3 * n];
            t.data[4 * o + 1] = this._tab[3 * n + 1];
            t.data[4 * o + 2] = this._tab[3 * n + 2];
            t.data[4 * o + 3] = 255;
            e.ctrl.t && n == e.ctrl.tranIndex && (t.data[4 * o + 3] = 0)
        }.bind(this));
        return t
    };
    e.prototype.mergeFrames = function(e, t) {
        var n = t;
        if (e) for (var o = 0; o < n.data.length; o += 4) if (0 == n.data[o + 3]) {
            n.data[o] = this._lastData.data[o];
            n.data[o + 1] = this._lastData.data[o + 1];
            n.data[o + 2] = this._lastData.data[o + 2];
            n.data[o + 3] = this._lastData.data[o + 3]
        }
        return n
    };
    e.prototype.dataUrl2SpriteFrame = function(e) {
        var t = new cc.Texture2D,
        n = new cc.SpriteFrame,
        o = new Image;
        o.src = e;
        t.initWithElement(o);
        n.setTexture(t);
        return n
    };
    e.prototype.date2SpriteFrame = function(e, t, n) {
        var o = new cc.RenderTexture,
        i = new cc.SpriteFrame;
        o.initWithData(e.data, cc.Texture2D.PixelFormat.RGBA8888, t, n);
        i.setTexture(o);
        return i
    };
    e.prototype.putImageDataJSB = function(e, t, n, o) {
        var i = this,
        a = function() {
            for (var o = 0; o < e.height; o++) for (var a = 0; a < e.width; a++) {
                var r = 4 * o * e.width + 4 * a,
                s = 4 * (o + n) * i._lastData.width + 4 * (a + t);
                if (0 != e.data[r + 3]) {
                    i._lastData.data[s] = e.data[r];
                    i._lastData.data[s + 1] = e.data[r + 1];
                    i._lastData.data[s + 2] = e.data[r + 2];
                    i._lastData.data[s + 3] = e.data[r + 3]
                }
            }
        },
        r = function() {
            for (var o = 0; o < i._lastData.height; o++) for (var a = 0; a < i._lastData.width; a++) {
                var r = 4 * o * i._lastData.width + 4 * a,
                s = 4 * (o - n) * e.width + 4 * (a - t),
                c = !1; (a < t || a > t + e.width) && (c = !0); (o < n || o > n + e.height) && (c = !0);
                if (c) {
                    i._lastData.data[r + 0] = 0;
                    i._lastData.data[r + 1] = 0;
                    i._lastData.data[r + 2] = 0;
                    i._lastData.data[r + 3] = 0
                } else {
                    i._lastData.data[r + 0] = e.data[s + 0];
                    i._lastData.data[r + 1] = e.data[s + 1];
                    i._lastData.data[r + 2] = e.data[s + 2];
                    i._lastData.data[r + 3] = e.data[s + 3]
                }
            }
        };
        4 == e.data[0] && 0 == e.data[1] && 0 == e.data[2] && 0 == e.data[3] || (1 == o.ctrl.disp || 0 == o.ctrl.disp ? a() : 2 == o.ctrl.disp ? r() : function() {
            for (var t = 0,
            n = 0; n < e.height; n += 2) {
                for (var o = 0,
                i = 0; i < e.width; i++) {
                    var a = 4 * n * e.width + 4 * i;
                    0 == e.data[a + 3] && o++
                }
                o / e.width > .1 && t++;
                if (t / (e.height / 2) > .6) return ! 0
            }
            return ! 1
        } () ? r() : a())
    };
    e.prototype.putImageDataWeb = function(e, t) {
        var n;
        if (1 == t.ctrl.disp || 0 == t.ctrl.disp) {
            this._context.putImageData(e, t.img.x, t.img.y, 0, 0, t.img.w, t.img.h);
            var o = this._context.getImageData(0, 0, this._canvas.width, this._canvas.height),
            i = this._lastData;
            n = this.mergeFrames(i, o)
        } else {
            this._context.clearRect(0, 0, this._canvas.width, this._canvas.height);
            this._context.putImageData(e, t.img.x, t.img.y, 0, 0, t.img.w, t.img.h);
            n = this._context.getImageData(0, 0, this._canvas.width, this._canvas.height)
        }
        this._context.putImageData(n, 0, 0);
        this._lastData = n;
        return this._canvas.toDataURL()
    };
    e.prototype.decodeFrame2Texture = function(e, t) {
        if (!this._context) {
            this._canvas = document.createElement("canvas");
            this._context = this._canvas.getContext("2d");
            this._canvas.width = e.img.w;
            this._canvas.height = e.img.h
        }
        var n = this.decodeFrame(e);
        this._delays[t] = e.ctrl.delay;
        this._lastData ? this.putImageDataJSB(n, e.img.x, e.img.y, e) : this._lastData = n;
        this._spriteFrames[t] = this.date2SpriteFrame(this._lastData, this._canvas.width, this._canvas.height);
        return this._spriteFrames[t]
    };
    e.prototype.read = function(e) {
        return this._view.slice(this._offset, this._offset += e)
    };
    e.prototype.getHeader = function() {
        this._info.header = "";
        this.read(6).forEach(function(e) {
            this._info.header += String.fromCharCode(e)
        }.bind(this))
    };
    e.prototype.getScrDesc = function() {
        var e = this.read(7);
        this._info.w = e[0] + (e[1] << 8);
        this._info.h = e[2] + (e[3] << 8);
        this._info.m = 1 & e[4] >> 7;
        this._info.cr = 7 & e[4] >> 4;
        this._info.s = 1 & e[4] >> 3;
        this._info.pixel = 7 & e[4];
        this._info.bgColor = e[5];
        this._info.radio = e[6];
        this._info.m && (this._info.colorTab = this.read(3 * (2 << this._info.pixel)));
        this.decode()
    };
    e.prototype.decode = function() {
        var e = [],
        t = this.read(1);
        switch (t[0]) {
            case 33:
                this.extension();
            break;
            case 44:
                t = this.read(9);
            this._frame.img = {
                x: t[0] + (t[1] << 8),
                y: t[2] + (t[3] << 8),
                w: t[4] + (t[5] << 8),
                h: t[6] + (t[7] << 8),
                colorTab: 0
            };
            this._frame.img.m = 1 & t[8] >> 7;
            this._frame.img.i = 1 & t[8] >> 6;
            this._frame.img.s = 1 & t[8] >> 5;
            this._frame.img.r = 3 & t[8] >> 3;
            this._frame.img.pixel = 7 & t[8];
            this._frame.img.m && (this._frame.img.colorTab = this.read(3 * (2 << this._frame.img.pixel)));
            this._frame.img.codeSize = this.read(1)[0];
            e = [];
            for (;;) {
                if (! (t = this.read(1))[0]) {
                    this._frame.img.srcBuf = e;
                    this.decode();
                    break
                }
                this.read(t[0]).forEach(function(t) {
                    e.push(t)
                })
            }
            break;
            case 59:
                console.log("The end.", this._offset, this.buffer.byteLength);
        }
    };
    e.prototype.extension = function() {
        var e = this.read(1);
        switch (e[0]) {
            case 255:
                if (11 != this.read(1)[0]) throw new Error("解析出错");
            this._info.appVersion = "";
            this.read(11).forEach(function(e) {
                this._info.appVersion += String.fromCharCode(e)
            }.bind(this));
            for (;;) {
                if (! (e = this.read(1))[0]) {
                    this.decode();
                    break
                }
                this.read(e[0])
            }
            break;
            case 249:
                if (4 != this.read(1)[0]) throw new Error("解析出错");
            e = this.read(4);
            this._frame = {};
            this._frame.ctrl = {
                disp: 7 & e[0] >> 2,
                i: 1 & e[0] >> 1,
                t: 1 & e[0],
                delay: e[1] + (e[2] << 8),
                tranIndex: e[3]
            };
            this._info.frames.push(this._frame);
            if (0 != this.read(1)[0]) throw new Error("解析出错");
            this.decode();
            break;
            case 254:
                if ((e = this.read(1))[0]) {
                this.read(e[0]).forEach(function(e) {
                    this._info.comment += String.fromCharCode(e)
                });
                0 == this.read(1)[0] && this.decode()
            }
        }
    };
    e.prototype.clear = function() {
        this._tab = null;
        this._view = null;
        this._frame = null;
        this._offset = 0;
        this._info = {
            header: "",
            frames: [],
            comment: ""
        };
        this._lastData = null;
        this._delays = [];
        this._spriteFrames = [];
        this._canvas = null;
        this._context = null
    };
    return e
} ();
n.GIF = r;
var s = function() {
    function e() {
        this.gifFrameMap = {}
    }
    e.getInstance = function() {
        if (!e.instance) {
            e.instance = new e;
            cc.loader.addDownloadHandlers({
                gif: cc.loader.downloader.extMap.binary
            });
            cc.loader.addLoadHandlers({
                gif: function(e, t) {
                    new r().handle(e, t)
                }
            })
        }
        return e.instance
    };
    e.prototype.preloadGif = function(e) {
        try {
            e.words && e.words.forEach(function(e) { - 1 != e.indexOf(".gif") && cc.loader.load(e.img,
                function() {})
            });
            e.classes && e.classes.forEach(function(e) { - 1 != e.indexOf(".gif") && cc.loader.load(e.img,
                function() {})
            })
        } catch(e) {
            cc.log(e)
        }
    };
    e.prototype.addItemFrame = function(e, t) {
        if (1 == this.has(e)) {
            var n = this.get(e);
            n.referenceCount++;
            n.frameData = t
        } else {
            var i = {
                referenceCount: 0,
                type: o.GIF,
                frame: {}
            };
            this.gifFrameMap[e] = i
        }
    };
    e.prototype.addItemType = function(e, t) {
        if (this.has(e)) this.get(e).type = t;
        else {
            var n = {
                referenceCount: 0,
                type: t,
                frame: null
            };
            this.gifFrameMap[e] = n
        }
    };
    e.prototype.add = function(e, t) {
        this.has(e) || (this.gifFrameMap[e] = t)
    };
    e.prototype.get = function(e) {
        return this.gifFrameMap[e]
    };
    e.prototype.has = function(e) {
        return null != this.gifFrameMap[e]
    };
    e.prototype.hasFrame = function(e) {
        var t = this.get(e);
        return null != t && null != t.frameData
    };
    e.prototype.relase = function(e) {
        if (this.has(e)) {
            this.gifFrameMap[e] = void 0;
            cc.loader.release(e)
        }
    };
    e.prototype.releaseAll = function() {
        for (var e in this.gifFrameMap) cc.loader.release(e);
        this.gifFrameMap = {}
    };
    e.instance = null;
    return e
} ();
n.GIFCache = s